﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class ThuocDAL
    {
        PhongMachTuDBDataContext db;
        public List<THUOC> LayDSThuoc()
        {
            db = new PhongMachTuDBDataContext();
            var q = from s in db.THUOCs
                    select s;
            return q.ToList();
        }
        public bool ThemThuoc(THUOC s)
        {

            try
            {
                db = new PhongMachTuDBDataContext();
                THUOC r = new THUOC();
                r.MaThuoc = s.MaThuoc;
                r.SoLuongTon = s.SoLuongTon;
                r.TenThuoc = s.TenThuoc;
                db.THUOCs.InsertOnSubmit(r);
                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool suaThuoc(THUOC s)
        {

            try
            {
                db = new PhongMachTuDBDataContext();
                THUOC r = db.THUOCs.Single(id => id.MaThuoc == s.MaThuoc);
                r.SoLuongTon = s.SoLuongTon;
                r.TenThuoc = s.TenThuoc;
                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool xoaThuoc(string MaThuoc)
        {
            try
            {
                db = new PhongMachTuDBDataContext();
                var delete = from d in db.THUOCs
                             where d.MaThuoc == MaThuoc
                             select d;
                if (delete.Count() > 0)
                {
                    db.THUOCs.DeleteOnSubmit(delete.FirstOrDefault());
                    db.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

    }
}
